//
//  NetRequestClass.m
//  袋鼠车管家
//
//  Created by czy001 on 17/4/28.
//  Copyright © 2017年 czy001. All rights reserved.
//

#import "NetRequestClass.h"

@implementation NetRequestClass

#pragma POST请求
+ (void) NetRequestPOSTWithRequestURL: (NSString *) requestURLString
                        WithParameter: (NSDictionary *) parameter
                 WithReturnValeuBlock: (ReturnValueBlock) block
                   WithErrorCodeBlock: (ErrorCodeBlock) errorBlock
                     WithFailureBlock: (FailureBlock) failureBlock
{
    //1.管理器
    
    AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
    session.responseSerializer = [AFHTTPResponseSerializer serializer];
    //    manager.requestSerializer.timeoutInterval = 5.0;
    // 加上这行代码，https ssl 验证。
    
//    AFSecurityPolicy *securityPolicy = [AFNetWorking_policy customSecurityPolicy];
//    //     allowInvalidCertificates 是否允许无效证书（也就是自建的证书），默认为NO
//    //     如果是需要验证自建证书，需要设置为YES
//    securityPolicy.allowInvalidCertificates = YES;
//    session.securityPolicy = securityPolicy;
    
    [session POST:requestURLString parameters:parameter progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSError *error=nil;
        NSDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:responseObject
                                                                options:NSJSONReadingMutableLeaves
                                                                  error:&error];
        block(dataDic); //自动返回主线程
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failureBlock(error);
    }];
}

#pragma GET请求
+ (void) NetRequestGETWithRequestURL: (NSString *) requestURLString
                       WithParameter: (NSDictionary *) parameter
                WithReturnValeuBlock: (ReturnValueBlock) block
                  WithErrorCodeBlock: (ErrorCodeBlock) errorBlock
                    WithFailureBlock: (FailureBlock) failureBlock
{
    AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
    
    [session POST:requestURLString parameters:parameter progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        block(responseObject); //自动返回主线程
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failureBlock(error);
    }];
}

+ (BOOL)isNull:(id)object{
    NSString *s = FORMAT(@"%@", object);
    BOOL isNull1 = [s isEqualToString:@"<null>"] || [s isEqualToString:@"(null)"] || [s isEqualToString:@"null"];
    BOOL isNull2 = [s isEqualToString:@"<NULL>"] || [s isEqualToString:@"(NULL)"] || [s isEqualToString:@"NULL"];
    return isNull1 || isNull2 || !object;
}

@end
